/*  Copyright 2003  Auri Marcelo Rizzo Vicenzi, Marcio Eduardo Delamaro, 			    Jose Carlos Maldonado
This file is part of Jabuti.
Jabuti is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation, either version 3 of the      
License, or (at your option) any later version.
Jabuti is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Jabuti.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.escel.jabuti.cmdtool;

import java.io.File;

import org.w3c.dom.Document;

import br.jabuti.project.JabutiProject;
import br.jabuti.util.ToolConstants;
import br.jabuti.util.XMLGen;
import br.jabuti.util.XMLPrettyPrinter;

/**
This scripts generates a XML file containing a testing report.
The testing report can be generated with respect to different levels,
such as, project, class, method, test set and test cases.
@version: 0.00001
@author: Auri Marcelo Rizzo Vincenzi
 */
public class JabutiReport {

    public static void usage() {
        System.out.println("java cmdtool.JabutiReport -p <project_file> -all -pr -cl -me -ts -tc [-o <report_file_name>] \n");
        System.out.println("      -p <project_file>       Specify the name of the Project to be used. The");
        System.out.println("                              Project must be a valid Project file (.jba) generated by");
        System.out.println("                              instrument the base class.");

        System.out.println("      -all                    Geraneta a complete report, including");
        System.out.println("                              information from project to method level, and the");
        System.out.println("                              test set and test case coverages.");

        System.out.println("      -pr                     Coverage summary by project.");

        System.out.println("      -cl                     Coverage summary by class.");

        System.out.println("      -me                     Coverage summary by method.");

        System.out.println("      -ts                     Coverage summary by test set.");

        System.out.println("      -tc                     Coverage summary by test case.");

        System.out.println("      -o <report_file_name>   The name of the testing report to be generated. If ommited\n" +
                "                              it is generated a file named jabuti_report.xml.");
        System.out.println("      [-d <DIR>]   Optional parameter. Specify the directory where the report will be create");

        System.out.println("\nCopyright (c) 2002\n");
    }

    public static void main(String args[]) throws Throwable {
        String relatName = new String("jabuti_report.xml");
        String projectName = null;
        String workDir = null;

        JabutiProject prj = null;

        boolean isPr = false;
        boolean isCl = false;
        boolean isMe = false;
        boolean isTs = false;
        boolean isTc = false;

        if (args.length > 0) {

            int i = 0;

            while ((i < args.length) && (args[i].startsWith("-"))) {
                // Recovering the project
                if (("-p".equals(args[i])) && (i < args.length - 1)) {
                    i++;
                    projectName = args[i];
                } // Report file name 
                else if (("-o".equals(args[i])) && (i < args.length - 1)) {
                    i++;
                    relatName = args[i];
                } // Complete summary
                else if ("-all".equals(args[i])) {
                    isPr = isCl = isMe = isTs = isTc = true;
                } // Project summary
                else if ("-pr".equals(args[i])) {
                    isPr = true;
                } // Class summary
                else if ("-cl".equals(args[i])) {
                    isPr = isCl = true;
                } // Method summary
                else if ("-me".equals(args[i])) {
                    isPr = isCl = isMe = true;
                } // Test set summary
                else if ("-ts".equals(args[i])) {
                    isTs = true;
                } // Test case summary
                else if ("-tc".equals(args[i])) {
                    isTs = isTc = true;
                } else if ("-d".equals(args[i])) {
                    i++;
                    workDir = args[i];
                } else {
                    System.out.println("Error: Unrecognized option: " + args[i]);
                    System.out.println("try java cmdtool.CoverageSummary -h for help.");
                    System.exit(0);
                }
                i++;
            }

            // Checking if all essential parameters are not null
            if (projectName == null) {
                System.out.println("Error: Missing parameter!!!");
                usage();
                System.exit(1);
            }

            try {
                File theFile = new File(workDir + File.separator + projectName);

                if (!theFile.isFile()) // verifica se existe
                {
                    System.out.println("File " + theFile.getName() + " not found");
                    System.exit(0);
                }

                prj = JabutiProject.reloadProj(theFile.toString(), true);

                if (relatName.length() > 0) {
                    if (!relatName.endsWith(".xml")) {
                        relatName = relatName + ".xml";
                    }
                }
                
                relatName = workDir + relatName;

                Document htmlDoc = null;
                try {
                    htmlDoc = XMLGen.customReport(prj, isPr, isCl, isMe, isTs, isTc);

                    XMLPrettyPrinter.writeDocument(htmlDoc,
                            relatName);
                    System.out.println("Report " + relatName + " successfully generated.");
                } catch (Exception pce) {
                    System.out.println("Error saving the file " + relatName + "!!!");
                    System.exit(1);
                }
            } catch (Exception e) {
                ToolConstants.reportException(e, ToolConstants.STDERR);
                System.exit(1);
            }
        } else {
            usage();
        }
    }
}
